package com.xxkk.gameCore.vo;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.xxkk.gameCore.proto.bo.AccountProBuf;

import java.util.List;

/**
 * @author xxkk
 * @version 1.0.0
 * @title Role
 * @description ʕ•ᴥ•ʔ
 * @create 2023/11/15 16:36
 **/
public class Role {
    private static final long oneDayInMillis = 24 * 60 * 60 * 1000; // 一天的毫秒数
    private String platformId;// 关联的平台id，必要的属性字段
    private long roleId;
    private String name;
    private int areaId;//区id
    private long lastLoginTime;
    private long lastRefreshLoginDaysTime;// 连续登陆刷新时间
    private int consecutiveLoginDays;// 连续登陆天数
    private int totalLoginDays;
//    private String platformChannel; 用不到

    /**
     * 刷新最后登陆时间
     * 玩家登陆时调用，刷新连续登陆天数和总登陆天数
     */
    public void refreshLastLoginTime() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastRefreshLoginDaysTime > oneDayInMillis) {
            consecutiveLoginDays++;
            totalLoginDays++;
            if (currentTime - lastRefreshLoginDaysTime > 2 * oneDayInMillis) {
                consecutiveLoginDays = 1;
            }
            lastRefreshLoginDaysTime = currentTime;
        }
        lastLoginTime = currentTime;
    }

    public void parseFrom(byte[] bytes) {
        try {
            AccountProBuf.RoleInfoProto proto = AccountProBuf.RoleInfoProto.parseFrom(bytes);
            this.platformId = proto.getPlatformId();
            this.roleId = proto.getRoleId();
            this.name = proto.getName();
            this.lastLoginTime = proto.getLastLoginTime();
            this.lastRefreshLoginDaysTime = proto.getLastRefreshLoginDaysTime();
            this.consecutiveLoginDays = proto.getConsecutiveLoginDays();
            this.totalLoginDays = proto.getTotalLoginDays();
            this.areaId = proto.getAreaId();
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException(e);
        }
    }

    public Message.Builder build() {
        AccountProBuf.RoleInfoProto.Builder builder = AccountProBuf.RoleInfoProto.newBuilder();
        builder.setRoleId(this.roleId);
        builder.setPlatformId(this.platformId);
        if (this.name != null)
            builder.setName(this.name);
        builder.setLastLoginTime(this.lastLoginTime);
        builder.setLastRefreshLoginDaysTime(this.lastRefreshLoginDaysTime);
        builder.setConsecutiveLoginDays(this.consecutiveLoginDays);
        builder.setTotalLoginDays(this.totalLoginDays);
        builder.setAreaId(this.areaId);
        return builder;
    }

    public String getPlatformId() {
        return platformId;
    }

    public void setPlatformId(String platformId) {
        this.platformId = platformId;
    }

    public long getRoleId() {
        return roleId;
    }

    public void setRoleId(long roleId) {
        this.roleId = roleId;
    }

    public int getAreaId() {
        return areaId;
    }

    public void setAreaId(int areaId) {
        this.areaId = areaId;
    }

    @Override
    public String toString() {
        return "Role{" +
                "platformId='" + platformId + '\'' +
                ", roleId=" + roleId +
                ", name='" + name + '\'' +
                ", areaId=" + areaId +
                ", lastLoginTime=" + lastLoginTime +
                ", lastRefreshLoginDaysTime=" + lastRefreshLoginDaysTime +
                ", consecutiveLoginDays=" + consecutiveLoginDays +
                ", totalLoginDays=" + totalLoginDays +
                '}';
    }
}
